package com.yxkj.railservicer.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yxkj.railservicer.R;
import com.yxkj.railservicer.pager.BasePager;
import com.yxkj.railservicer.pager.Cancelpager;
import com.yxkj.railservicer.pager.CompleteBasePager;
import com.yxkj.railservicer.pager.CompletePager;
import com.yxkj.railservicer.util.JSONUtil;
import com.yxkj.railservicer.util.ScreenUtils;
import com.yxkj.railservicer.util.SysApplicationUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;

/**
 * Created by YX_BOSS on 2017/4/4.
 */

public class CompleteActivity extends Activity implements View.OnClickListener {

    private static final String TAG = "TAG";
    private Button btn_setting_exit;
    private ImageView iv_main_setting;
    private ImageView iv_main_back;
    private TextView tv_title;

    public List<CompleteBasePager> completeBasePagers;

    private RadioGroup rg_complete;
    private RadioButton rb_complete_complete;
    private RadioButton rb_complete_cancel;

    private ViewPager vp_complete;

    private OkHttpClient client;
    private JSONUtil jsonUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complete);
        ScreenUtils.setTranStatus(this, true, Color.parseColor("#2387f5"));
        SysApplicationUtil.getInstance().addActivity(this);

        initData();
        initEvent();
    }

    private void initData() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        btn_setting_exit = (Button) findViewById(R.id.btn_setting_exit);
        iv_main_setting = (ImageView) findViewById(R.id.iv_main_setting);
        iv_main_back = (ImageView) findViewById(R.id.iv_main_back);

        rg_complete = (RadioGroup) findViewById(R.id.rg_complete);
        rb_complete_complete = (RadioButton) findViewById(R.id.rb_complete_complete);
        rb_complete_cancel = (RadioButton) findViewById(R.id.rb_complete_cancel);
        vp_complete = (ViewPager) findViewById(R.id.vp_complete);


        completeBasePagers = new ArrayList<>();
    }

    private void initEvent() {
        tv_title.setText("今日已完成");
        iv_main_setting.setVisibility(View.INVISIBLE);
        iv_main_back.setOnClickListener(this);

        completeBasePagers.add(new CompletePager(this));
        completeBasePagers.add(new Cancelpager(this));
        //给viewPager添加适配器
        vp_complete.setAdapter(new MyPagerAdapter());
        //按钮变化监听
        rg_complete.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
        //默认第一页
        vp_complete.setCurrentItem(0);
        //默认第一个按钮
        rg_complete.check(R.id.rb_complete_complete);
        //页面变化监听
        vp_complete.setOnPageChangeListener(new MyOnPageChangeListener());
        //加载第一页数据
        completeBasePagers.get(0).initData();
        completeBasePagers.get(1).initData();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.iv_main_back:
                finish();
                break;
        }
    }

    /**
     * viewPager适配器
     */
    private class MyPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return completeBasePagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            CompleteBasePager completeBasePager = completeBasePagers.get(position);
            View rootView = completeBasePager.rootView;
            container.addView(rootView);

            return rootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    /**
     * 按钮变化监听
     */
    private class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rb_complete_complete:
                    vp_complete.setCurrentItem(0);
                    rb_complete_complete.setTextColor(Color.parseColor("#248cf5"));
                    rb_complete_cancel.setTextColor(Color.parseColor("#aaaaaa"));
                    break;
                case R.id.rb_complete_cancel:
                    vp_complete.setCurrentItem(1);
                    rb_complete_cancel.setTextColor(Color.parseColor("#248cf5"));
                    rb_complete_complete.setTextColor(Color.parseColor("#aaaaaa"));
                    break;
            }
        }
    }

    /**
     * 页面变化监听
     */
    private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            //滑动某页加载对应页的数据
            completeBasePagers.get(position).initData();
            //手动滑动时候找到对应的RadioButton
            ((RadioButton) rg_complete.getChildAt(position)).setChecked(true);

        }

        @Override
        public void onPageScrollStateChanged(int state) {
//            Log.e(TAG, "onPageScrollStateChanged: " + state);
        }
    }

    //动态显示订单总数
    public void getCount(CompleteBasePager pager, String count) {
        if (count == null) {
            count = "0";
        }

        if (pager instanceof CompletePager) {
            rb_complete_complete.setText("已完成 [" + count + "]");
        }  else {
            rb_complete_cancel.setText("已取消 [" + count + "]");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SysApplicationUtil.getInstance().deleteActivity(this);
    }
}
